package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PStoreProductRelation;
import com.ruoyi.system.domain.bo.PStoreProductRelationBo;
import com.ruoyi.system.domain.vo.PStoreProductRelationVo;
import com.ruoyi.system.mapper.PStoreProductRelationMapper;
import com.ruoyi.system.service.IPStoreProductRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品点赞和收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@RequiredArgsConstructor
@Service
public class PStoreProductRelationServiceImpl implements IPStoreProductRelationService {

    private final PStoreProductRelationMapper baseMapper;

    /**
     * 查询商品点赞和收藏
     */
    @Override
    public PStoreProductRelationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品点赞和收藏列表
     */
    @Override
    public TableDataInfo<PStoreProductRelationVo> queryPageList(PStoreProductRelationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreProductRelation> lqw = buildQueryWrapper(bo);
        Page<PStoreProductRelationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品点赞和收藏列表
     */
    @Override
    public List<PStoreProductRelationVo> queryList(PStoreProductRelationBo bo) {
        LambdaQueryWrapper<PStoreProductRelation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreProductRelation> buildQueryWrapper(PStoreProductRelationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreProductRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, PStoreProductRelation::getUid, bo.getUid());
        lqw.eq(bo.getProductId() != null, PStoreProductRelation::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PStoreProductRelation::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), PStoreProductRelation::getCategory, bo.getCategory());
        return lqw;
    }

    /**
     * 新增商品点赞和收藏
     */
    @Override
    public Boolean insertByBo(PStoreProductRelationBo bo) {
        PStoreProductRelation add = BeanUtil.toBean(bo, PStoreProductRelation.class);
        add.setUid(LoginHelper.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品点赞和收藏
     */
    @Override
    public Boolean updateByBo(PStoreProductRelationBo bo) {
        PStoreProductRelation update = BeanUtil.toBean(bo, PStoreProductRelation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreProductRelation entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品点赞和收藏
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<PStoreProductRelationVo> queryPageLeftList(PStoreProductRelationBo bo, PageQuery pageQuery) {
        QueryWrapper<PStoreProductRelationVo> lqw = Wrappers.query();
        lqw.eq(bo.getUid() != null, "r.uid", bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), "r.type", bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), "r.category", bo.getCategory());
        Page<PStoreProductRelationVo> result = baseMapper.selectVoLeftPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean queryFlagById(Long id) {
        List<PStoreProductRelation> res = baseMapper.selectList(new LambdaQueryWrapper<PStoreProductRelation>().eq(PStoreProductRelation::getProductId, id)
            .eq(PStoreProductRelation::getUid, LoginHelper.getUserId())
            .eq(PStoreProductRelation::getDelFlag, "0")
            .eq(PStoreProductRelation::getCategory, "0"));
        if (CollectionUtil.isNotEmpty(res)) {
            return true;
        }

        return false;
    }

    @Override
    public void cancleFlag(Long id) {
        Long userId = LoginHelper.getUserId();
        baseMapper.cancelOne(id, userId, PublishConstants.COMMON_NO);
    }

    @Override
    public boolean querySeckillFlagById(Long id) {
        List<PStoreProductRelation> res = baseMapper.selectList(new LambdaQueryWrapper<PStoreProductRelation>().eq(PStoreProductRelation::getProductId, id)
            .eq(PStoreProductRelation::getUid, LoginHelper.getUserId())
            .eq(PStoreProductRelation::getDelFlag, "0")
            .eq(PStoreProductRelation::getCategory, "1"));
        if (CollectionUtil.isNotEmpty(res)) {
            return true;
        }

        return false;
    }

    @Override
    public void cancleSeckillFlag(Long id) {
        Long userId = LoginHelper.getUserId();
        baseMapper.cancelOne(id, userId, PublishConstants.COMMON_YES);
    }
}
